package it.tcm.TeacherClassMate.facade;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import it.tcm.TeacherClassMate.beans.MailBean;
import it.tcm.TeacherClassMate.dao.MailDao;
import it.tcm.TeacherClassMate.persistent.HibernateUtil;

public class MailManageBase {

	 	private Logger logger = Logger.getLogger(this.getClass().getName());   
	    private MailDao mDao = new MailDao();
	  
	    public MailBean getMailById(Integer id) {
		    Session session = null;
		    MailBean mail = null;
			try {
				session = HibernateUtil.createNewSessionAndTransaction();
				mDao.setSession(session);
				mail = mDao.findById(id, false);
			    HibernateUtil.commitTransactionAndCloseSession(session);
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    logger.error("Error in reading mail "  + id.toString(), e);
			} finally {
			}
			return mail;
	    }
	    
	   
	    
	    public Integer save(MailBean m)
	    {
	  	  Session session = null;
	  	  try 
	  	  {
	  		session = HibernateUtil.createNewSessionAndTransaction();
	  		mDao.setSession(session);
	  		mDao.attachDirty(m);
	  		HibernateUtil.commitTransactionAndCloseSession(session);
	  	  } 
	  	  catch (Exception e) 
	  	  {
	  		  logger.error("Errore in save mail ", e);
	  		  HibernateUtil.rollbackTransaction(session);
	  	  } 	   	  
	  	  return m.getId();
	    }
	    
	    
	    public boolean delete(MailBean m) {  
	  	  Session session = null;
	  	  try 
	  	  {
	  		session = HibernateUtil.createNewSessionAndTransaction();
	  		mDao.setSession(session);
	  		mDao.delete(m);
	  		HibernateUtil.commitTransactionAndCloseSession(session);
	  	  } 
	  	  catch (Exception e) 
	  	  {
	  		  logger.error("Errore in delete mail ", e);
	  		  HibernateUtil.rollbackTransaction(session);
	  	  } 	   	  
	  	  return true;
	    }
	    
	    
	    public MailBean getFirstMailByNoSentAndNoLockType1() {
	    	Session session = null;
	    	List<MailBean> mailList = null;
	    	MailBean mail = null;
			try {
				 session = HibernateUtil.createNewSessionAndTransaction();
				 ArrayList<Order> order = new ArrayList<Order>();
				 order.add(Order.asc("id"));		 				    
				 ArrayList<Criterion> criterion = new ArrayList<Criterion>();
				 session.createCriteria(MailBean.class);               
				 Criterion critIdElementoArc = Restrictions.eq("sent", "0").ignoreCase();      
				 criterion.add(critIdElementoArc);			
				 Criterion critIdElementoArc2 = Restrictions.eq("lock", "0").ignoreCase();
				 criterion.add(critIdElementoArc2);	
				 Criterion critIdElementoArc3 = Restrictions.eq("type", "1").ignoreCase();
				 criterion.add(critIdElementoArc3);	
				 Criterion critIdElementoArc4 = Restrictions.le("failed", 3);
				 criterion.add(critIdElementoArc4);	
				 mDao.setSession(session);
				 mailList = mDao.findByCriteria(order, (Criterion[]) criterion.toArray(new Criterion[criterion.size()]));	
				 for(MailBean m : mailList) {
			    	 mail = m;
			    	 break;
			     }
			     HibernateUtil.commitTransactionAndCloseSession(session);  
			    
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    logger.error("Error in reading mail", e);
			}
			return mail;
	    }	
	    
	    public MailBean getFirstMailByNoSentAndNoLockType2() {
	    	Session session = null;
	    	List<MailBean> mailList = null;
	    	MailBean mail = null;
			try {
				 session = HibernateUtil.createNewSessionAndTransaction();
				 ArrayList<Order> order = new ArrayList<Order>();
				 order.add(Order.asc("id"));		 				    
				 ArrayList<Criterion> criterion = new ArrayList<Criterion>();
				 session.createCriteria(MailBean.class);               
				 Criterion critIdElementoArc = Restrictions.eq("sent", "0").ignoreCase();      
				 criterion.add(critIdElementoArc);			
				 Criterion critIdElementoArc2 = Restrictions.eq("lock", "0").ignoreCase();
				 criterion.add(critIdElementoArc2);	
				 Criterion critIdElementoArc3 = Restrictions.eq("type", "2").ignoreCase();
				 criterion.add(critIdElementoArc3);	
				 Criterion critIdElementoArc4 = Restrictions.le("failed", 3);
				 criterion.add(critIdElementoArc4);	
				 mDao.setSession(session);
				 mailList = mDao.findByCriteria(order, (Criterion[]) criterion.toArray(new Criterion[criterion.size()]));	
				 for(MailBean m : mailList) {
			    	 mail = m;
			    	 break;
			     }
			     HibernateUtil.commitTransactionAndCloseSession(session);  
			    
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    logger.error("Error in reading mail", e);
			}
			return mail;
	    }	
	   
	   

}
